package com.example.casper.jnuexperiment2025;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

public class MapFragment extends Fragment {

    private Marker marker;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        TextureMapView mapView = view.findViewById(R.id.map_view_content);
        TencentMap tencentMap = mapView.getMap();
        tencentMap.getUiSettings().setZoomControlsEnabled(true);

        LatLng centerPoint = new LatLng(22.26,113.54);


        float maxZoom = tencentMap.getMaxZoomLevel();

        CameraUpdate cameraSigma =
                CameraUpdateFactory.newCameraPosition(new CameraPosition(
                        centerPoint,
                        maxZoom,
                        0,
                        0
                ));
        tencentMap.moveCamera(cameraSigma);


        Bitmap icon = scaleBitmapToDp(R.drawable.jnu, 48);

        marker = tencentMap.addMarker(new MarkerOptions(centerPoint));
        marker.setTitle("Jnu Zhuhai");
        marker.setIcon(BitmapDescriptorFactory.fromBitmap(icon));


        tencentMap.setOnCameraChangeListener(new TencentMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                // 缩放中不处理
            }

            @Override
            public void onCameraChangeFinished(CameraPosition cameraPosition) {
                float zoom = cameraPosition.zoom;

                int sizeDp;
                if (zoom >= 18) {
                    sizeDp = 72;   // 放大时 72dp
                } else if (zoom >= 16) {
                    sizeDp = 56;
                } else if (zoom >= 14) {
                    sizeDp = 48;
                } else {
                    sizeDp = 36;   // 缩小时 36dp
                }

                Bitmap scaled = scaleBitmapToDp(R.drawable.jnu, sizeDp);
                marker.setIcon(BitmapDescriptorFactory.fromBitmap(scaled));

            }
        });

        tencentMap.setOnMarkerClickListener(new TencentMap.OnMarkerClickListener(){
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(getActivity(), marker.getTitle(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });


        marker.setClickable(true);

        return view;
    }


    private Bitmap scaleBitmapToDp(int resId, int dp) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
        float density = getResources().getDisplayMetrics().density;

        int sizePx = (int) (dp * density);  // dp → px

        return Bitmap.createScaledBitmap(bitmap, sizePx, sizePx, true);
    }

}
